package com.hspedu.homework;

import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/5
 * @time 18:54
 */
public class Homework02 {
    public static void main(String[] args) {
        String name = "jack";
        String pwd = "123456";
        String email = "jack@sohu.com";
        try {
            userRegister(name,pwd,email);
            System.out.println("恭喜你注册成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void userRegister(String username, String pwd, String email) {
        if (!(username != null && pwd != null && email != null)){
            throw new RuntimeException("参数不能为空");
        }
        //1.验证用户名
        if (!(username.length()>=2 && username.length() <= 4 )){
            throw new RuntimeException("用户名长度为2或3或4");
        }
        //2.验证密码
        if (!(pwd.length() == 6 && isDigital(pwd) )){
            throw new RuntimeException("密码长度必须为6且必须全为数字");
        }
        //3.验证邮箱
        int index1 = email.lastIndexOf("@");
        int index2 = email.lastIndexOf(".");
        if (!(index1 > 0 && index1 < index2)){
            throw new RuntimeException("邮箱中必须包含@和.   并且@在 . 的前面");
        }
    }

    public static boolean isDigital(String pwd){
        char[] chars = pwd.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (!(chars[i] >= '0' && chars[i] <= '9')){
                return false;
            }
        }
        return true;
    }

    public static void regist() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scan.nextLine();
        if (!(username.length() == 2 ||
                username.length() == 3 ||
                username.length() == 4)) {
            throw new RuntimeException("用户名长度为2或3或4");
        }
        System.out.println("请输入你的密码：");
        int password = scan.nextInt();
        if (!(password > 100000 || password <= 999999)) {
            throw new RuntimeException("密码的长度为六，且必须全部为数字");
        }
        System.out.println("请输入你的邮箱:");
        String email = scan.nextLine();
        char[] chars = email.toCharArray();
        int count = 0;
        int count2 = 0;
        int index1 = -1;
        int index2 = -1;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '@') {
                count++;
                index1 = i;
            }
            if (chars[i] == '.') {
                count2++;
                index2 = i;
            }
        }
        if (count == 0) {
            throw new RuntimeException("邮箱中必须包含 @ ");
        }
        if (count2 == 0) {
            throw new RuntimeException("邮箱中必须包含 . ");
        }
        if (index1 >= index2) {
            throw new RuntimeException("@ 必须在 . 的前面");
        }
    }
}